﻿Imports FastReport.Report
Imports DevExpress.XtraGrid.Views.Grid
Public Class frmEditPRForm
    Public Primary_Key As Integer = -1
    Public Primary_Detail_Key = -1
    Public Curr_RowID As Integer = 0
    Public DocNO As Integer = 0
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Report_Name As String
    Dim DT_pr_header As New DataTable
    Dim DT_Department As New DataTable
    Dim r As Integer
#Region "Function"
    Sub Load_Doctype()
        Try
            Dim DT_DocType As New DataTable
            Strsql = "Select doc_type_ID,doc_type_name from doc_type order by doc_type_name "
            DT_DocType = _mysql.GetMYSQLDataTable(Strsql, "doc_type")
            With CboDocType

                .DataSource = DT_DocType
                .DisplayMember = "doc_type_name"
                .ValueMember = "doc_type_ID"
                .Text = DT_DocType.Rows(0).Item("doc_type_name")
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Doctype"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Department()
        Try

            Strsql = "Select dep_id,dep_name from departments order by dep_name "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "departments")
            With CboDepartments

                .DataSource = DT_Department
                .DisplayMember = "dep_name"
                .ValueMember = "dep_id"
                '.Text = DT_Department.Rows(0).Item("dep_name")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Doc_Status()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select status_id,status_name from doc_status  order by status_name "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "doc_status")
            With CboStatus

                .DataSource = DT_Department
                .DisplayMember = "status_name"
                .ValueMember = "status_id"

            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Doc_Status"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Data()

        Try


            Strsql = "select doc_no,doc_date,doc_department,departments.dep_name,doc_department,doc_remarks,doc_recevie_date "
            Strsql = Strsql & " ,doc_status,doc_staff,pr_header.doc_type,doc_lastupdate,doc_place,doc_status.status_name from pr_header "
            Strsql = Strsql & " left Join departments ON pr_header.doc_department = departments.dep_id  "
            Strsql = Strsql & " Left Join doc_status ON pr_header.doc_status = doc_status.status_id "
            Strsql = Strsql & " where doc_id = " & Primary_Key & ""

            DT_pr_header = _mysql.GetMYSQLDataTable(Strsql, "pr_header")
            If DT_pr_header.Rows.Count = 0 Then Exit Sub
            Me.TxtDocNo.Text = DT_pr_header.Rows(0).Item("doc_no").ToString
            Me.TxtPlace.Text = DT_pr_header.Rows(0).Item("doc_place").ToString
            Me.TxtRemark.Text = DT_pr_header.Rows(0).Item("doc_remarks").ToString
            Me.TxtStaff.Text = DT_pr_header.Rows(0).Item("doc_staff").ToString
            Me.DtpDocDate.DateTime = DT_pr_header.Rows(0).Item("doc_date").ToString
            Me.DTPReceiveDate.DateTime = DT_pr_header.Rows(0).Item("doc_recevie_date").ToString
            Me.Group.Text = DT_pr_header.Rows(0).Item("dep_name").ToString
            CboStatus.Text = DT_pr_header.Rows(0).Item("status_name").ToString
            CboDepartments.Text = DT_pr_header.Rows(0).Item("dep_name").ToString
            CboDocType.SelectedValue = DT_pr_header.Rows(0).Item("doc_type").ToString
            '

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try



    End Sub
    Function Calc_TotoalPrice() As Double

        Try
            Strsql = "select Sum(totalprice) as sumprice from pr_detail "
            Strsql = Strsql & " where(doc_head_id = " & Primary_Key & ")"

            Calc_TotoalPrice = _mysql.MySQLExecuteScalar(Strsql)





        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Detail"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    Sub Load_Detail()
        Try
            Strsql = "select doc_detail_id,doc_head_id,company_id,doc_rowid,item_type_id,itemname,serialno,band,model,priceperunit,vat,priceincludevat,"
            Strsql = Strsql & " drawqty,receiveqty,totalprice,staff,remarks,purchase_date,mdf_date,warantry,companyname from pr_detail "
            Strsql = Strsql & " left join itemtype on item_type_id = itemnumber "
            Strsql = Strsql & " Left Join vendorcompany ON pr_detail.company_id = vendorcompany.companyid "
            Strsql = Strsql & " where(doc_head_id = " & Primary_Key & ")"
            Dim DT_pr_detail As New DataTable
            DT_pr_detail = _mysql.GetMYSQLDataTable(Strsql, "pr_detail")
            GridControl1.DataSource = DT_pr_detail




        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Detail"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Update_Data()
        Try
            Strsql = "Update pr_header set  "
            Strsql = Strsql & " doc_no = '" & Me.TxtDocNo.Text & "'"
            Strsql = Strsql & " ,doc_date= '" & Pn_Framework.MysqlDateTimeFormat(Me.DtpDocDate.DateTime, False) & "'"
            Strsql = Strsql & " ,doc_department= " & CboDepartments.SelectedValue
            Strsql = Strsql & " ,doc_remarks= '" & Me.TxtRemark.Text & "'"
            Strsql = Strsql & " ,doc_recevie_date= '" & Pn_Framework.MysqlDateTimeFormat(Me.DTPReceiveDate.DateTime, False) & "'"
            Strsql = Strsql & " ,doc_status =" & Me.CboStatus.SelectedValue
            Strsql = Strsql & " ,doc_staff= '" & Me.TxtStaff.Text & "'"
            Strsql = Strsql & " ,doc_lastupdate= '" & Pn_Framework.MysqlDateTimeFormat(Now, False) & "'"
            Strsql = Strsql & " ,doc_place = '" & Me.TxtPlace.Text & "'"
            Strsql = Strsql & " ,doc_type = " & Me.CboDocType.SelectedValue
            Strsql = Strsql & "  where doc_id = " & Primary_Key
            Select Case _mysql.MySQLExecute(Strsql)
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Update_Data"
                        .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
                Case 0
                    MsgBox("ไม่พบ Record ที่ต้องการ update", MsgBoxStyle.Exclamation, "ผลการปรับปรุงข้อมูล")

                Case Is > 0
                    MsgBox("ปรับปรุงเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
                    Me.DialogResult = Windows.Forms.DialogResult.OK
            End Select

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Update_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region

    Private Sub frmEditReceiveForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
            If MsgBox("คุณต้องการออกโดยไม่บันทึกการแก้ไขใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Me.DialogResult = Windows.Forms.DialogResult.Cancel
                e.Cancel = True
            End If
        End If
        With frmPRList
            .MdiParent = frmmain
            .WindowState = FormWindowState.Maximized
            .Show()
        End With

    End Sub
    Private Sub EditReceiveForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Department()
        Load_Doc_Status()
        Load_Doctype()
        Load_Data()
        Load_Detail()
        Me.TxtTotolPrice.EditValue = Calc_TotoalPrice()
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
        With frmPRList
            .MdiParent = frmmain
            .WindowState = FormWindowState.Maximized
            .Show()
        End With
    End Sub

    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MsgBox("คุณต้องการลบข้อมูล 'สินค้า' รหัส " & Primary_Detail_Key & " ออกจากระบบหรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub



        Try
            Strsql = "delete from pr_detail where doc_detail_id = " & Primary_Detail_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnDelete_Click : delete detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            Else
                MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบ")
                Load_Detail()
                Me.TxtTotolPrice.EditValue = Calc_TotoalPrice()
            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try


    End Sub

    Private Sub CboDepartments_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub BtnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrint.Click
        Try
            Strsql = "SELECT report_id,report_name,report_formname,report_filename,report_group from report_template where report_id = 3"
            Dim DT_Reportlist As New DataTable
            DT_Reportlist = _mysql.GetMYSQLDataTable(Strsql, "report_template")
            Report_Name = DT_Reportlist.Rows(0).Item("report_filename").ToString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

            With frmPrintDrawForm
                .Primary_Key = Primary_Key
                .Report_Name = Report_Name
                .MdiParent = frmmain
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnPrint_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub


    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridControl1.Click

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Primary_Detail_Key = -1 Then Exit Sub
        frmEditPRItem.Primary_Key = Primary_Detail_Key
        If frmEditPRItem.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub

        Load_Detail()
        Me.TxtTotolPrice.EditValue = Calc_TotoalPrice()
    End Sub



    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Dim Gv As GridView = sender
        Primary_Detail_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("doc_detail_id"))
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Update_Data()
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Dispose()
        With frmPRList
            .MdiParent = frmmain
            .WindowState = FormWindowState.Maximized
            .Show()
        End With
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click

        Curr_RowID = GridView1.RowCount
        frmNewPRItem.New_Rows = Curr_RowID
        frmNewPRItem.Primary_Key = Primary_Key
        If frmNewPRItem.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Try
            Load_Detail()
            Me.TxtTotolPrice.EditValue = Calc_TotoalPrice()

            frmNewPRItem.Dispose()
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnAdd_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnPrintPR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrintPR.Click
        Try
            Strsql = "SELECT report_id,report_name,report_formname,report_filename,report_group from report_template where report_id = 1"
            Dim DT_Reportlist As New DataTable
            DT_Reportlist = _mysql.GetMYSQLDataTable(Strsql, "report_template")
            Report_Name = DT_Reportlist.Rows(0).Item("report_filename").ToString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

            With frmPrintDrawForm
                .Primary_Key = Primary_Key
                .Report_Name = Report_Name
                .MdiParent = frmmain
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnPrintPR_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnClone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClone.Click
        If MsgBox("คุณต้องคัดลอกข้อมูลสินค้า Record ที่ " & Primary_Detail_Key & " เป็น Record ใหม่ใช่หรือไม่?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการคัดลอก") = MsgBoxResult.No Then Exit Sub

        Try
            Strsql = "Insert into pr_detail (doc_head_id,doc_rowid,productname,serialno,band,model,totalpriceprice,drawqty,receiveqty,staff,remarks,purchase_date,mdf_date,warantry,priceincludevat,vat,priceperunit,nonprice,nonmfd,nonbuydate,nonvat,imagepart) "
            Strsql = Strsql & " select  doc_head_id,doc_rowid,productname,serialno,band,model,totalpriceprice,drawqty,receiveqty,staff,remarks,purchase_date,mdf_date,warantry,priceincludevat,vat,priceperunit,nonprice,nonmfd,nonbuydate,nonvat,imagepart FROM pr_detail where doc_detail_id = " & Primary_Detail_Key



            'Strsql = "delete from pr_detail where doc_detail_id = " & Primary_Key
            If _mysql.MySQLExecute(Strsql) < 0 Then
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnClone_Click : Copy detail"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                    Exit Sub
                End With


            End If
            Load_Detail()
            Me.TxtTotolPrice.EditValue = Calc_TotoalPrice()
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnClone_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnRepairForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRepairForm.Click
        Try
            Strsql = "SELECT report_id,report_name,report_formname,report_filename,report_group from report_template where report_id = 4"
            Dim DT_Reportlist As New DataTable
            DT_Reportlist = _mysql.GetMYSQLDataTable(Strsql, "report_template")
            Report_Name = DT_Reportlist.Rows(0).Item("report_filename").ToString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try

            With frmPrintDrawForm
                .Primary_Key = Primary_Key
                .Report_Name = Report_Name
                .MdiParent = frmmain
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnRepairForm_Click"
                .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub PanelControl2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelControl2.Paint

    End Sub
End Class